package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.MeasuringChainPlatformInfoDTO;
import org.springblade.modules.platform.dto.MeasuringChainPlatformTypeDTO;
import org.springblade.modules.platform.entity.MeasuringChainPlatformInfo;
import org.springblade.modules.platform.entity.MeasuringChainPlatformType;
import org.springblade.modules.platform.entity.MeasuringPlatformType;
import org.springblade.modules.platform.mapper.MeasuringChainPlatformInfoMapper;
import org.springblade.modules.platform.mapper.MeasuringChainPlatformTypeMapper;
import org.springblade.modules.platform.service.IMeasuringChainPlatformTypeService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2023-07-10
 */
@Service
public class MeasuringChainPlatformTypeServiceImpl extends ServiceImpl<MeasuringChainPlatformTypeMapper, MeasuringChainPlatformType> implements IMeasuringChainPlatformTypeService {

    @Autowired
    MeasuringChainPlatformInfoMapper measuringChainPlatformInfoMapper;
    @Override
    public Long installMeasuringChainPlatformType(MeasuringChainPlatformTypeDTO measuringChainPlatformTypeDTO) {
        Long id = measuringChainPlatformTypeDTO.getId();
        if(id==null){
            MeasuringChainPlatformType copy = BeanUtil.copy(measuringChainPlatformTypeDTO, MeasuringChainPlatformType.class);
            baseMapper.insert(copy);
            id = copy.getId();
        }
        MeasuringChainPlatformTypeDTO.MeasuringChainPlatformInfoDTO measuringChainPlatformInfoDTO2 = measuringChainPlatformTypeDTO.getMeasuringChainPlatformInfoDTO();
        if(measuringChainPlatformInfoDTO2==null){
            return id;
        }
        if(measuringChainPlatformInfoDTO2.getParentPlatformId()==null){//基础侧台
            Integer sort = 0;
            MeasuringChainPlatformInfo copy1 = BeanUtil.copy(measuringChainPlatformInfoDTO2, MeasuringChainPlatformInfo.class);
            copy1.setSort(sort);
            copy1.setMeasuringTypeId(id);
            copy1.setxCoordinate("40");
            copy1.setyCoordinate("40");
            measuringChainPlatformInfoMapper.insert(copy1);
        }else {
            MeasuringChainPlatformInfo measuringChainPlatformInfo = measuringChainPlatformInfoMapper.selectOne(Wrappers.<MeasuringChainPlatformInfo>query().lambda()
                    .eq(MeasuringChainPlatformInfo::getMeasuringPlatformId, measuringChainPlatformInfoDTO2.getParentPlatformId())
                    .eq(MeasuringChainPlatformInfo::getMeasuringTypeId,id));
            MeasuringChainPlatformInfo copy1 = BeanUtil.copy(measuringChainPlatformInfoDTO2, MeasuringChainPlatformInfo.class);
            copy1.setSort(measuringChainPlatformInfo.getSort()+1);
            copy1.setMeasuringTypeId(id);


            copy1.setxCoordinate(String.valueOf(Integer.parseInt(measuringChainPlatformInfo.getxCoordinate()) + 80));
            copy1.setyCoordinate(measuringChainPlatformInfo.getyCoordinate());
            measuringChainPlatformInfoMapper.insert(copy1);
        }
        return id;
    }

    @Override
    public void updataMeasuringChainPlatformType(MeasuringChainPlatformInfoDTO measuringChainPlatformInfoDTO) {
        MeasuringChainPlatformInfo measuringChainPlatformInfo = measuringChainPlatformInfoMapper.selectById(measuringChainPlatformInfoDTO.getId());
        measuringChainPlatformInfo.setxAxis(measuringChainPlatformInfoDTO.getxAxis());
        measuringChainPlatformInfo.setyAxis(measuringChainPlatformInfoDTO.getyAxis());
        measuringChainPlatformInfo.setxCoordinate(measuringChainPlatformInfoDTO.getX());
        measuringChainPlatformInfo.setyCoordinate(measuringChainPlatformInfoDTO.getY());
        measuringChainPlatformInfo.setPreviousDistance(measuringChainPlatformInfoDTO.getPreviousDistance());
        measuringChainPlatformInfoMapper.updateById(measuringChainPlatformInfo);
    }

    @Override
    public void deleteMeasuringChainPlatformType(Long id) {
        baseMapper.deleteById(id);
        measuringChainPlatformInfoMapper.delete(Wrappers.<MeasuringChainPlatformInfo>query().lambda().eq(MeasuringChainPlatformInfo::getMeasuringTypeId,id));
    }
    @Override
    public void removeSideSill(Long id) {
        measuringChainPlatformInfoMapper.deleteById(id);
    }
    @Override
    public IPage<MeasuringChainPlatformInfo>  getListByPage(Integer measuringTypeCode,Long measuringChainId, String measuringPlatformName, Query query) {
        MeasuringChainPlatformType measuringChainPlatformType = baseMapper.selectOne(Wrappers.<MeasuringChainPlatformType>query().lambda()
                .eq(MeasuringChainPlatformType::getMeasuringChainId, measuringChainId)
                .eq(MeasuringChainPlatformType::getMeasuringTypeCode, measuringTypeCode));
        if(measuringChainPlatformType==null){
            return null;
        }
        LambdaQueryWrapper<MeasuringChainPlatformInfo> eq = Wrappers.<MeasuringChainPlatformInfo>query().lambda().eq(MeasuringChainPlatformInfo::getMeasuringTypeId, measuringChainPlatformType.getId());
        if(!StringUtil.isEmpty(measuringPlatformName)){
            eq.like(MeasuringChainPlatformInfo::getMeasuringPlatformName,measuringPlatformName);
        }
       IPage<MeasuringChainPlatformInfo> measuringChainPlatformInfoIPage = measuringChainPlatformInfoMapper.selectPage(Condition.getPage(query),eq);

/*        List<MeasuringChainPlatformInfo> measuringChainPlatformInfos = measuringChainPlatformInfoMapper.selectList(eq);*/

        return measuringChainPlatformInfoIPage;

    }

    @Override
    public MeasuringPlatformType getFatherPlatform(Long id) {

        List<MeasuringChainPlatformInfo> measuringChainPlatformInfos = measuringChainPlatformInfoMapper.selectList(Wrappers.<MeasuringChainPlatformInfo>query().lambda()
                .eq(MeasuringChainPlatformInfo::getMeasuringTypeId, id).orderByDesc(MeasuringChainPlatformInfo::getSort));
        if(Func.isEmpty(measuringChainPlatformInfos)){
            return null;
        }
        MeasuringPlatformType measuringPlatformType = new MeasuringPlatformType();
        Long measuringPlatformId = measuringChainPlatformInfos.get(0).getMeasuringPlatformId();
        String measuringPlatformName = measuringChainPlatformInfos.get(0).getMeasuringPlatformName();
        measuringPlatformType.setMeasuringPlatformName(measuringPlatformName);
        measuringPlatformType.setId(measuringPlatformId);
        return measuringPlatformType;
    }

    @Override
    public List<MeasuringChainPlatformType> getMeasuringChainPlatformTypeList(Long measuringChainId) {
        return baseMapper.selectList(Wrappers.<MeasuringChainPlatformType>query().lambda()
                .eq(MeasuringChainPlatformType::getMeasuringChainId, measuringChainId));
    }
}
